//
//  HEBindSelectCell.m
//  HealthFamily
//
//  Created by 吴欧 on 2021/5/5.
//  Copyright © 2021 honglutec. All rights reserved.
//

#import "HEBindSelectCell.h"
#import "BRDatePickerView.h"
#import "BRStringPickerView.h"

@interface HEBindSelectCell ()<QMUITextFieldDelegate>

@property(nonatomic, strong) UIButton *leftLabel;
@property (nonatomic, strong) BRDatePickerView *datePickerView;
@property(nonatomic, strong) QMUITextField *textField;

@property(nonatomic, strong) UIImageView *arrowView;

@end


@implementation HEBindSelectCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
     if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
         self.selectionStyle = UITableViewCellSelectionStyleNone;
         
         self.qmui_borderPosition = QMUIViewBorderPositionBottom;
         self.qmui_borderWidth = 1;
         self.qmui_borderColor = CSplitLineColor;
        [self initUI];
     }
     return self;;
 }

- (void)initUI {
    _leftLabel = [UIButton new];
    [self.contentView addSubview:_leftLabel];
    [_leftLabel setTitle:@"手机号码" forState:UIControlStateNormal];
    _leftLabel.userInteractionEnabled = NO;
    _leftLabel.titleLabel.font = DYFont(17);
//    _leftLabel.imageEdgeInsets = UIEdgeInsetsMake(0,-15, 0, 0);
    [_leftLabel setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_leftLabel setImage:[UIImage imageNamed:@"3"] forState:UIControlStateNormal];
    [_leftLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    _leftLabel.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    
    _textField = [[QMUITextField alloc]init];
    _textField.userInteractionEnabled = NO;
    _textField.maximumTextLength = 500;
    _textField.placeholder = @"请输入内容";
    _textField.font = DYFont(17);
    _textField.layer.cornerRadius = 3;
    _textField.textInsets = UIEdgeInsetsMake(0, 10, 0, 10);
    _textField.clearButtonMode = UITextFieldViewModeNever;
    _textField.delegate = self;
    [self.contentView addSubview: _textField];
    _textField.layer.borderWidth = 1;
    _textField.layer.borderColor = CSplitLineColor.CGColor;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tfClick)];
    [self.contentView addGestureRecognizer:tap];
    
    _arrowView = [UIImageView new];
    _arrowView.contentMode = UIViewContentModeScaleAspectFill;
    _arrowView.image = IMAGE_NAMED(@"go_gray");
    [self.contentView addSubview:_arrowView];
    
    [_arrowView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.contentView);
        make.right.equalTo(self.contentView).mas_offset(-25);
        make.width.mas_offset(8);
        make.height.mas_offset(12);
    }];
     
     [_leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
         make.centerY.equalTo(self.contentView);
         make.left.equalTo(self.contentView).mas_offset(15);
         make.width.mas_offset([_leftLabel.titleLabel.text widthForFont:DYFont(15)] + 10);
      }];
    
    [_leftLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    
    [_textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).mas_offset(10);
        make.left.equalTo(_leftLabel.mas_right).mas_offset(5);
        make.right.equalTo(self.contentView).mas_offset(-10);
        make.height.mas_offset(36);
        make.bottom.equalTo(self.contentView).mas_offset(-10);
    }];
}

- (void)tfClick {
    if (self.fiterModel.isCloesPK) {
        return;
    }
    [[IQKeyboardManager sharedManager] resignFirstResponder];
    switch (self.fiterModel.pickerType) {
        case GDPickerType_String:
        {
            [self createStringPicker];
        }
            break;
        case GDPickerType_Date:
        {
            [self createPicker];
        }
            break;
        default:
            break;
    }
  
}

- (void)createStringPicker {
    BRStringPickerView *stringPickerView = [[BRStringPickerView alloc]init];
    stringPickerView.pickerMode = BRStringPickerComponentSingle;
    stringPickerView.title = self.fiterModel.leftTitle;
    stringPickerView.dataSourceArr = self.fiterModel.pickerDataArray;
    stringPickerView.selectValue = [self.formModel valueForKey:self.fiterModel.tableKey];
    
    KWeakSelf
    stringPickerView.resultModelBlock = ^(BRResultModel * _Nullable resultModel) {
        self.textField.text = resultModel.value;
        [self.formModel setValue:resultModel.value forKey:self.fiterModel.tableKey];
        if ([weakSelf.delegate respondsToSelector:@selector(cellSelectHandler:index:)]) {
            [weakSelf.delegate cellSelectHandler:self index:resultModel.index];
        }
    };
    
    // 自定义弹框样式
    BRPickerStyle *customStyle = [BRPickerStyle pickerStyleWithThemeColor:CNavBgColor];
    stringPickerView.pickerStyle = customStyle;
    [stringPickerView show];
}

- (void)createPicker{
    BRDatePickerView *datePickerView = [[BRDatePickerView alloc]init];
    datePickerView.pickerMode = self.fiterModel.pickerDateType;
    datePickerView.title = @"";
    
    datePickerView.minDate = self.fiterModel.minDate;
    datePickerView.maxDate = self.fiterModel.maxDate;
    
    
//    datePickerView.selectDate = self.birthtimeSelectDate;
    datePickerView.isAutoSelect = YES;
    datePickerView.resultBlock = ^(NSDate *selectDate, NSString *selectValue) {
        self.textField.text = selectValue;
        [self.formModel setValue:selectValue forKey:self.fiterModel.tableKey];
    };
 
    // 自定义弹框样式
    BRPickerStyle *customStyle = [BRPickerStyle pickerStyleWithThemeColor:CNavBgColor];
    datePickerView.pickerStyle = customStyle;
    [datePickerView show];
}

- (void)refreshContent:(GDFiterCellModel *)fiterModel formModel:(GDFormModel *)formModel indexPath:(NSIndexPath *)indexPath {
    _delegate = formModel.proxyVC;
//    _titleLb.text  =fiterModel.leftTitle;
    _textField.text = [formModel valueForKey:fiterModel.tableKey];
    _textField.placeholder = fiterModel.placeHolder;
    
    
    [_leftLabel setTitle:fiterModel.leftTitle forState:UIControlStateNormal];
    [_leftLabel setImage:(fiterModel.isHasMustSigns?[UIImage imageNamed:@"3"]: nil) forState:UIControlStateNormal];
}

#pragma mark - delegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    return NO;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
